#!/bin/bash
# 微服务停止脚本

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

echo "========================================="
echo "  停止微服务"
echo "========================================="
echo ""

# 停止所有PingService实例
for pid_file in /tmp/ping_service_*.pid; do
    if [ -f "$pid_file" ]; then
        PID=$(cat "$pid_file")
        PORT=$(basename "$pid_file" .pid | sed 's/ping_service_//')
        
        echo -n "停止 PingService:$PORT (PID=$PID)... "
        
        if kill -0 $PID 2>/dev/null; then
            kill -TERM $PID
            sleep 1
            
            if kill -0 $PID 2>/dev/null; then
                echo -e "${RED}✗ 进程未响应，强制终止${NC}"
                kill -9 $PID
            else
                echo -e "${GREEN}✓ 已停止${NC}"
            fi
            
            rm -f "$pid_file"
        else
            echo -e "${RED}✗ 进程不存在${NC}"
            rm -f "$pid_file"
        fi
    fi
done

echo ""
echo "========================================="
echo "✅ 所有服务已停止"
echo "========================================="
